
***************************************************
*Figure 1
***************************************************
***************************************************
*Figure 1: 1/6 and 5/6
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

collapse wage sa (sum) e, by(pnr age_q) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************

foreach y of varlist wage sa {
forvalues s=2/2 {
preserve
keep if age_q>=21 & age_q<29
local spec_num=`s'
if `y'==wage local title="Employment"
if `y'==wage local fig_num=5
if `y'==sa   local title="Social Assistance"
if `y'==sa local fig_num=1

local min_wage=0.49
local max_wage=0.59
local min_sa  =0.09
local max_sa  =0.19
if `spec_num'==2 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"
if `spec_num'==2 local spec_name="2nd order polynomial spline"
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec' [aw=e] if resp==0 ,  vce(cluster pnr)

predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 
collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)
g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
twoway (rarea `y'_min `y'_max age_q if age_month_q<24.12, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>25.04, color(gs13)) ///
	   (line yhat age_q if age_month_q<24.12 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
   	   (line yhat age_q if age_month_q>25.04 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
	   (scatter `y' age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14)) ///
	   (scatter `y' age_q if `resp', msymbol(o) mlcolor(gs3) mfcolor(gs16)) ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in months", height(5))      ///
	     xline(24.958333) xline(25.291667, lpattern(dash)) scheme(s1mono) legen(off) name(fig1_`fig_num', replace) ///
		 subtitle("No control", margin(b=4))

graph save fig1_`fig_num'  $figures/fig1_`fig_num', replace
graph export $figures/fig1_`fig_num'.pdf, name(fig1_`fig_num') replace
graph export $figures/fig1_`fig_num'.png, name(fig1_`fig_num') replace

restore
}
}


***************************************************
*Figure 1: 2/6 and 6/6
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q year month b_month b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g age_q0_4= age_q0^4
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3
g dXage_q0_4=d*age_q0_4

*********************************************************************


eststo clear
local model=0
foreach y of varlist wage sa {
forvalues s=2/2 {
preserve
keep if age_q>=21 & age_q<29

reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res


*local y="wage"
local spec_num=`s'
if `y'==wage local title="Employment (residuals)"
if `y'==sa   local title="Social Assistance (residuals)"
if `y'==wage local fig_num=6
if `y'==sa local fig_num=2

local min_wage=-0.05
local max_wage=0.05
local min_sa  =-0.05
local max_sa  =0.05
if `spec_num'==2 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"
if `spec_num'==2 local spec_name="2nd order polynomial spline"

local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec'  [aw=e] if resp==0 , vce(cluster pnr)

predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 
collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)
g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
twoway (rarea `y'_min `y'_max age_q if age_month_q<24.12, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>25.04, color(gs13)) ///
	   (line yhat age_q if age_month_q<24.12 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
   	   (line yhat age_q if age_month_q>25.04 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
	   (scatter `y' age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14)) ///
	   (scatter `y' age_q if `resp', msymbol(o) mlcolor(gs3) mfcolor(gs16)) ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in months", height(5))      ///
	     xline(24.958333) xline(25.291667, lpattern(dash)) scheme(s1mono) legen(off) name(fig1_`fig_num', replace) ///
 		 subtitle("Control for time and cohort", margin(b=4))

graph save fig1_`fig_num'  $figures/fig1_`fig_num', replace
graph export $figures/fig1_`fig_num'.pdf, name(fig1_`fig_num') replace
graph export $figures/fig1_`fig_num'.png, name(fig1_`fig_num') replace

restore
}
}

***************************************************
*Figure 1: 3/6 and 4/6
***************************************************


use "$workdata/cohort", clear /* see $data_do/cohort.do */

g e=1

collapse sa (sum) e, by(pnr age_q year month b_month b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************
*No control, 3/6
*********************************************************************

eststo clear
local model=0
foreach y of varlist sa {
forvalues s=2/2 {
preserve
keep if age_q>=21 & age_q<29
keep if b_year>=1975 & b_year<=1981

local spec_num=`s'
if `y'==sa   local title="Social Assistance"
local min_wage=-0.05
local max_wage=0.05
local min_sa  =0.10
local max_sa  =0.20
if `spec_num'==2 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"
if `spec_num'==2 local spec_name="2nd order polynomial spline"
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec'  [aw=e] if resp==0 , vce(cluster pnr)

predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 
collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)
g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
twoway (rarea `y'_min `y'_max age_q if age_month_q<24.12, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>25.04, color(gs13)) ///
	   (line yhat age_q if age_month_q<24.12 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
   	   (line yhat age_q if age_month_q>25.04 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
	   (scatter `y' age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14)) ///
	   (scatter `y' age_q if `resp', msymbol(o) mlcolor(gs3) mfcolor(gs16)) ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in months", height(5))      ///
	     xline(24.958333) xline(25.291667, lpattern(dash)) scheme(s1mono) legen(off) name(fig1_3, replace) ///
 		 subtitle("No control", margin(b=4))

graph save fig1_3  $figures/fig1_3, replace
graph export $figures/fig1_3.pdf, name(fig1_3) replace
graph export $figures/fig1_3.png, name(fig1_3) replace

restore
}
}

*********************************************************************
*Time and Cohort, 4/6 
*********************************************************************

eststo clear
local model=0
foreach y of varlist sa {
forvalues s=2/2 {
preserve
keep if age_q>=21 & age_q<29
keep if b_year>=1975 & b_year<=1981
reg `y' i.year##i.month i.b_year##i.b_month [aw=e]
predict res, r
replace `y'=res

local spec_num=`s'
if `y'==sa   local title="Social Assistance (residuals)"
local min_wage=-0.05
local max_wage=0.05
local min_sa  =-0.05
local max_sa  =0.05
if `spec_num'==2 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"
if `spec_num'==2 local spec_name="2nd order polynomial spline"
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec'  [aw=e] if resp==0 , vce(cluster pnr)
predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 
collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)
g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
twoway (rarea `y'_min `y'_max age_q if age_month_q<24.12, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>25.04, color(gs13)) ///
	   (line yhat age_q if age_month_q<24.12 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
   	   (line yhat age_q if age_month_q>25.04 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
	   (scatter `y' age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14)) ///
	   (scatter `y' age_q if `resp', msymbol(o) mlcolor(gs3) mfcolor(gs16)) ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in months", height(5))      ///
	     xline(24.958333) xline(25.291667, lpattern(dash)) scheme(s1mono) legen(off) name(fig1_4, replace) ///
		 subtitle("Control for time and cohort", margin(b=4))

graph save fig1_4  $figures/fig1_4, replace
graph export $figures/fig1_4.pdf, name(fig1_4) replace
graph export $figures/fig1_4.png, name(fig1_4) replace

restore
}
}

graph use $figures/fig1_1
graph use $figures/fig1_2
graph combine ///
      fig1_1 fig1_2 ///
      , cols(2) rows(2) ysize(8) xsize(10) altshrink  scheme(s1mono) ///
	  title("SA Receipt, Baseline Selection" , size(medsmall)) name(fig1_row1, replace)
graph use $figures/fig1_3
graph use $figures/fig1_4
graph combine ///
      fig1_3 fig1_4 ///
      , cols(2) rows(2) ysize(8) xsize(10) altshrink  scheme(s1mono) ///
	  title("SA Receipt, Cohort-tracking Selection" , size(medsmall)) name(fig1_row2, replace)
graph use $figures/fig1_5
graph use $figures/fig1_6
graph combine ///
      fig1_5 fig1_6 ///
      , cols(2) rows(2) ysize(8) xsize(10) altshrink  scheme(s1mono) ///
	  title("Employment, Baseline Selection" , size(medsmall)) name(fig1_row3, replace)
graph combine ///
	  fig1_row1 fig1_row2 fig1_row3 ///
	  , cols(1) rows(3) ysize(18) xsize(14) altshrink graphregion(margin(zero)) scheme(s1mono)  ///
	  note("	Note: Trend lines: 2nd order polynomial spline", tstyle(size(tiny))) name(fig1, replace) 
graph save fig1  $figures/fig1, replace
graph export $figures/fig1.pdf, name(fig1) replace
graph export $figures/fig1.png, name(fig1) replace

***************************************************
*end: Figure 1
***************************************************

